<template>
	<view class="container">
		<view class="forms">
			<view class="game-title">挂机吧！传奇</view>
			<input type="text" class="game-input-box"  v-model="username" placeholder="用户名"/>
			<input type="password" class="game-input-box" v-model="password" placeholder="密码"/>
			<button  class="login-btn" @click="pwdLogin">登录</button>
			<navigator class="register" url="/pages/register/register">没有账号？立即注册</navigator>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				password: "",
				username: ""
			}
		},
		onShow() {
			// #ifdef H5
			document.onkeydown = event => {
				var e = event || window.event;
				if (e && e.keyCode == 13) { //回车键的键值为13
					this.pwdLogin()
				}
			};
			// #endif
		},
		methods: {
			/**
			 * 密码登录
			 */
			async pwdLogin() {
				
				if (!this.username) {
					return uni.showToast({
						title: '请输入用户名',
						icon: 'none'
					});
				}
				
				if (!this.password) {
					return uni.showToast({
						title: '请输入密码',
						icon: 'none'
					});
				}
				
				//登录
				let loginData = await this.$request({
					name : 'account',
					method : 'login',
					username : this.username,
					password : this.password
				})
				
				uni.setStorageSync('GAME_TOKEN',loginData.data.uid);
				
				//登录完成检测该账号是否有角色
				let playerData = await this.$request({
					name : 'player',
					method : 'check_player'
				})
				
				if(playerData.data.player > 0){
					uni.reLaunch({
						url:'/pages/loading/loading'
					})
				}else{
					uni.reLaunch({
						url:'/pages/create_player/create_player'
					})
				}

	
				
				
				

			}
		}
	}
</script>

<style lang="scss" scoped>
	// @import "@/uni_modules/uni-id-pages/common/login-page.scss";
	
	.container{
		width:100vw;
		height:100vh;
		display: flex;
		justify-content: center;
		align-items: center;
		.forms{
			width:400px;
			.game-title{
				text-align: center;
				padding-bottom:20px;
				font-size: 24px;
				font-weight: bold;
			}
			
			.login-btn{
				@include game-btn;
				height:40px;
				margin-bottom:10px;
				color:#abb2bf
			}
			.register{
				text-decoration: underline;
				display: inline;
				&:hover{
					color:#ffffff
				}
			}
		}
	}
</style>
